home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / PNG Library 0.80 / PNGRUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-29  |  32.6 KB  |  1,275 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngrutil.c - utilities to read a png file
  3.  
  4.    libpng 1.0 beta 2 - version 0.8
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  7.    August 20, 1995
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #include "png.h"
  12.  
  13. /* grab an uint 32 from a buffer */
  14. png_uint_32
  15. png_get_uint_32(png_byte *buf)
  16. {
  17.    png_uint_32 i;
  18.  
  19.    i = ((png_uint_32)(*buf) << 24) +
  20.       ((png_uint_32)(*(buf + 1)) << 16) +
  21.       ((png_uint_32)(*(buf + 2)) << 8) +
  22.       (png_uint_32)(*(buf + 3));
  23.  
  24.    return i;
  25. }
  26.  
  27. /* grab an uint 16 from a buffer */
  28. png_uint_16
  29. png_get_uint_16(png_byte *buf)
  30. {
  31.    png_uint_16 i;
  32.  
  33.    i = ((png_uint_16)(*buf) << 8) +
  34.       (png_uint_16)(*(buf + 1));
  35.  
  36.    return i;
  37. }
  38.  
  39. /* read data, and run it through the crc */
  40. void
  41. png_crc_read(png_struct *png_ptr, png_byte *buf, png_uint_32 length)
  42. {
  43.    png_read_data(png_ptr, buf, length);
  44.    png_calculate_crc(png_ptr, buf, length);
  45. }
  46.  
  47. /* skip data, but calcuate the crc anyway */
  48. void
  49. png_crc_skip(png_struct *png_ptr, png_uint_32 length)
  50. {
  51.    png_uint_32 i;
  52.  
  53.    for (i = length; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size)
  54.    {
  55.       png_read_data(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  56.       png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  57.    }
  58.    if (i)
  59.    {
  60.       png_read_data(png_ptr, png_ptr->zbuf, i);
  61.       png_calculate_crc(png_ptr, png_ptr->zbuf, i);
  62.    }
  63. }
  64.  
  65. /* read and check the IDHR chunk */
  66. void
  67. png_handle_IHDR(png_struct *png_ptr, png_info *info, png_uint_32 length)
  68. {
  69.    png_byte buf[13];
  70.    png_uint_32 width, height;
  71.    int bit_depth, color_type, compression_type, filter_type;
  72.    int interlace_type;
  73.  
  74.    /* check the length */
  75.    if (length != 13)
  76.       png_error(png_ptr, "Invalid IHDR chunk");
  77.  
  78.    png_crc_read(png_ptr, buf, 13);
  79.  
  80.    width = png_get_uint_32(buf);
  81.    height = png_get_uint_32(buf + 4);
  82.    bit_depth = buf[8];
  83.    color_type = buf[9];
  84.    compression_type = buf[10];
  85.    filter_type = buf[11];
  86.    interlace_type = buf[12];
  87.  
  88.    /* check for width and height valid values */
  89.    if (width == 0 || height == 0)
  90.       png_error(png_ptr, "Invalid Width or Height Found");
  91.  
  92.    /* check other values */
  93.    if (bit_depth != 1 && bit_depth != 2 &&
  94.       bit_depth != 4 && bit_depth != 8 &&
  95.       bit_depth != 16)
  96.       png_error(png_ptr, "Invalid Bit Depth Found");
  97.  
  98.    if (color_type < 0 || color_type == 1 ||
  99.       color_type == 5 || color_type > 6)
  100.       png_error(png_ptr, "Invalid Color Type Found");
  101.  
  102.    if (color_type == PNG_COLOR_TYPE_PALETTE &&
  103.       bit_depth == 16)
  104.       png_error(png_ptr, "Found Invalid Color Type and Bit Depth Combination");
  105.  
  106.    if ((color_type == PNG_COLOR_TYPE_RGB ||
  107.       color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  108.       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  109.       bit_depth < 8)
  110.       png_error(png_ptr, "Found Invalid Color Type and Bit Depth Combination");
  111.  
  112.    if (interlace_type > 1)
  113.       png_error(png_ptr, "Found Invalid Interlace Value");
  114.  
  115.    if (compression_type > 0)
  116.       png_error(png_ptr, "Found Invalid Compression Value");
  117.  
  118.    if (filter_type > 0)
  119.       png_error(png_ptr, "Found Invalid Filter Value");
  120.  
  121.    /* set internal variables */
  122.    png_ptr->width = width;
  123.    png_ptr->height = height;
  124.    png_ptr->bit_depth = bit_depth;
  125.    png_ptr->interlaced = interlace_type;
  126.    png_ptr->color_type = color_type;
  127.  
  128.    /* find number of channels */
  129.    switch (png_ptr->color_type)
  130.    {
  131.       case 0:
  132.       case 3:
  133.          png_ptr->channels = 1;
  134.          break;
  135.       case 2:
  136.          png_ptr->channels = 3;
  137.          break;
  138.       case 4:
  139.          png_ptr->channels = 2;
  140.          break;
  141.       case 6:
  142.          png_ptr->channels = 4;
  143.          break;
  144.    }
  145.    /* set up other useful info */
  146.    png_ptr->pixel_depth = png_ptr->bit_depth *
  147.       png_ptr->channels;
  148.    png_ptr->rowbytes = ((png_ptr->width *
  149.       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  150.    /* call the IHDR callback (which should just set up info) */
  151.    png_read_IHDR(png_ptr, info, width, height, bit_depth,
  152.       color_type, compression_type, filter_type, interlace_type);
  153. }
  154.  
  155. /* read and check the palette */
  156. void
  157. png_handle_PLTE(png_struct *png_ptr, png_info *info, png_uint_32 length)
  158. {
  159.    int num, i;
  160.    png_color *palette;
  161.  
  162.    if (length % 3)
  163.       png_error(png_ptr, "Invalid Palette Chunk");
  164.  
  165.    num = (int)length / 3;
  166.    palette = (png_color *)png_malloc(png_ptr, num * sizeof (png_color));
  167.    for (i = 0; i < num; i++)
  168.    {
  169.       png_byte buf[3];
  170.  
  171.       png_crc_read(png_ptr, buf, 3);
  172.       /* don't depend upon png_color being any order */
  173.       palette[i].red = buf[0];
  174.       palette[i].green = buf[1];
  175.       palette[i].blue = buf[2];
  176.    }
  177.    png_ptr->palette = palette;
  178.    png_ptr->num_palette = num;
  179.    png_read_PLTE(png_ptr, info, palette, num);
  180. }
  181.  
  182. #if defined(PNG_READ_gAMA_SUPPORTED)
  183. void
  184. png_handle_gAMA(png_struct *png_ptr, png_info *info, png_uint_32 length)
  185. {
  186.    png_uint_32 igamma;
  187.    float gamma;
  188.    png_byte buf[4];
  189.  
  190.    if (length != 4)
  191.    {
  192.       png_crc_skip(png_ptr, length);
  193.       return;
  194.    }
  195.  
  196.    png_crc_read(png_ptr, buf, 4);
  197.    igamma = png_get_uint_32(buf);
  198.    /* check for zero gamma */
  199.    if (!igamma)
  200.       return;
  201.  
  202.    gamma = (float)igamma / (float)100000.0;
  203.    png_read_gAMA(png_ptr, info, gamma);
  204.    png_ptr->gamma = gamma;
  205. }
  206. #endif
  207.  
  208. #if defined(PNG_READ_sBIT_SUPPORTED)
  209. void
  210. png_handle_sBIT(png_struct *png_ptr, png_info *info, png_uint_32 length)
  211. {
  212.    int slen;
  213.    png_byte buf[4];
  214.  
  215.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  216.       slen = 3;
  217.    else
  218.       slen = png_ptr->channels;
  219.  
  220.    if (length != (png_uint_32)slen)
  221.    {
  222.       png_crc_skip(png_ptr, length);
  223.       return;
  224.    }
  225.  
  226.    png_crc_read(png_ptr, buf, length);
  227.    if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  228.    {
  229.       png_ptr->sig_bit.red = buf[0];
  230.       png_ptr->sig_bit.green = buf[1];
  231.       png_ptr->sig_bit.blue = buf[2];
  232.       png_ptr->sig_bit.alpha = buf[3];
  233.    }
  234.    else
  235.    {
  236.       png_ptr->sig_bit.gray = buf[0];
  237.       png_ptr->sig_bit.alpha = buf[1];
  238.    }
  239.    png_read_sBIT(png_ptr, info, &(png_ptr->sig_bit));
  240. }
  241. #endif
  242.  
  243. #if defined(PNG_READ_cHRM_SUPPORTED)
  244. void
  245. png_handle_cHRM(png_struct *png_ptr, png_info *info, png_uint_32 length)
  246. {
  247.    png_byte buf[4];
  248.    png_uint_32 v;
  249.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  250.  
  251.    if (length != 32)
  252.    {
  253.       png_crc_skip(png_ptr, length);
  254.       return;
  255.    }
  256.  
  257.    png_crc_read(png_ptr, buf, 4);
  258.    v = png_get_uint_32(buf);
  259.    white_x = (float)v / (float)100000.0;
  260.  
  261.    png_crc_read(png_ptr, buf, 4);
  262.    v = png_get_uint_32(buf);
  263.    white_y = (float)v / (float)100000.0;
  264.  
  265.    png_crc_read(png_ptr, buf, 4);
  266.    v = png_get_uint_32(buf);
  267.    red_x = (float)v / (float)100000.0;
  268.  
  269.    png_crc_read(png_ptr, buf, 4);
  270.    v = png_get_uint_32(buf);
  271.    red_y = (float)v / (float)100000.0;
  272.  
  273.    png_crc_read(png_ptr, buf, 4);
  274.    v = png_get_uint_32(buf);
  275.    green_x = (float)v / (float)100000.0;
  276.  
  277.    png_crc_read(png_ptr, buf, 4);
  278.    v = png_get_uint_32(buf);
  279.    green_y = (float)v / (float)100000.0;
  280.  
  281.    png_crc_read(png_ptr, buf, 4);
  282.    v = png_get_uint_32(buf);
  283.    blue_x = (float)v / (float)100000.0;
  284.  
  285.    png_crc_read(png_ptr, buf, 4);
  286.    v = png_get_uint_32(buf);
  287.    blue_y = (float)v / (float)100000.0;
  288.  
  289.    png_read_cHRM(png_ptr, info,
  290.       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  291. }
  292. #endif
  293.  
  294. #if defined(PNG_READ_tRNS_SUPPORTED)
  295. void
  296. png_handle_tRNS(png_struct *png_ptr, png_info *info, png_uint_32 length)
  297. {
  298.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  299.    {
  300.       if (length > png_ptr->num_palette)
  301.       {
  302.          png_crc_skip(png_ptr, length);
  303.          return;
  304.       }
  305.  
  306.       png_ptr->trans = png_malloc(png_ptr, length);
  307.       png_crc_read(png_ptr, png_ptr->trans, length);
  308.       png_ptr->num_trans = (int)length;
  309.    }
  310.    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  311.    {
  312.       png_byte buf[6];
  313.  
  314.       if (length != 6)
  315.       {
  316.          png_crc_skip(png_ptr, length);
  317.          return;
  318.       }
  319.  
  320.       png_crc_read(png_ptr, buf, length);
  321.       png_ptr->num_trans = 3;
  322.       png_ptr->trans_values.red = png_get_uint_16(buf);
  323.       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  324.       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  325.    }
  326.    else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  327.    {
  328.       png_byte buf[6];
  329.  
  330.       if (length != 2)
  331.       {
  332.          png_crc_skip(png_ptr, length);
  333.          return;
  334.       }
  335.  
  336.       png_crc_read(png_ptr, buf, 2);
  337.       png_ptr->num_trans = 1;
  338.       png_ptr->trans_values.gray = png_get_uint_16(buf);
  339.    }
  340.    else
  341.       png_error(png_ptr, "Invalid tRNS chunk");
  342.  
  343.    png_read_tRNS(png_ptr, info, png_ptr->trans, png_ptr->num_trans,
  344.       &(png_ptr->trans_values));
  345. }
  346. #endif
  347.  
  348. #if defined(PNG_READ_bKGD_SUPPORTED)
  349. void
  350. png_handle_bKGD(png_struct *png_ptr, png_info *info, png_uint_32 length)
  351. {
  352.    int truelen;
  353.    png_byte buf[6];
  354.  
  355.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  356.       truelen = 1;
  357.    else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  358.       truelen = 6;
  359.    else
  360.       truelen = 2;
  361.  
  362.    if (length != (png_uint_32)truelen)
  363.    {
  364.       png_crc_skip(png_ptr, length);
  365.       return;
  366.    }
  367.  
  368.    png_crc_read(png_ptr, buf, length);
  369.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  370.       png_ptr->background.index = buf[0];
  371.    else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  372.       png_ptr->background.gray = png_get_uint_16(buf);
  373.    else
  374.    {
  375.       png_ptr->background.red = png_get_uint_16(buf);
  376.       png_ptr->background.green = png_get_uint_16(buf + 2);
  377.       png_ptr->background.blue = png_get_uint_16(buf + 4);
  378.    }
  379.  
  380.    png_read_bKGD(png_ptr, info, &(png_ptr->background));
  381. }
  382. #endif
  383.  
  384. #if defined(PNG_READ_hIST_SUPPORTED)
  385. void
  386. png_handle_hIST(png_struct *png_ptr, png_info *info, png_uint_32 length)
  387. {
  388.    int num, i;
  389.  
  390.    if (length != 2 * png_ptr->num_palette)
  391.    {
  392.       png_crc_skip(png_ptr, length);
  393.       return;
  394.    }
  395.  
  396.    num = (int)length / 2;
  397.    png_ptr->hist = png_malloc(png_ptr, num * sizeof (png_uint_16));
  398.    for (i = 0; i < num; i++)
  399.    {
  400.       png_byte buf[2];
  401.  
  402.       png_crc_read(png_ptr, buf, 2);
  403.       png_ptr->hist[i] = png_get_uint_16(buf);
  404.    }
  405.    png_read_hIST(png_ptr, info, png_ptr->hist);
  406. }
  407. #endif
  408.  
  409. #if defined(PNG_READ_pHYs_SUPPORTED)
  410. void
  411. png_handle_pHYs(png_struct *png_ptr, png_info *info, png_uint_32 length)
  412. {
  413.    png_byte buf[9];
  414.    png_uint_32 res_x, res_y;
  415.    int unit_type;
  416.  
  417.    if (length != 9)
  418.    {
  419.       png_crc_skip(png_ptr, length);
  420.       return;
  421.    }
  422.  
  423.    png_crc_read(png_ptr, buf, 9);
  424.  
  425.    res_x = png_get_uint_32(buf);
  426.    res_y = png_get_uint_32(buf + 4);
  427.    unit_type = buf[8];
  428.    png_read_pHYs(png_ptr, info, res_x, res_y, unit_type);
  429. }
  430. #endif
  431.  
  432. #if defined(PNG_READ_oFFs_SUPPORTED)
  433. void
  434. png_handle_oFFs(png_struct *png_ptr, png_info *info, png_uint_32 length)
  435. {
  436.    png_byte buf[9];
  437.    png_uint_32 offset_x, offset_y;
  438.    int unit_type;
  439.  
  440.    if (length != 9)
  441.    {
  442.       png_crc_skip(png_ptr, length);
  443.       return;
  444.    }
  445.  
  446.    png_crc_read(png_ptr, buf, 9);
  447.  
  448.    offset_x = png_get_uint_32(buf);
  449.    offset_y = png_get_uint_32(buf + 4);
  450.    unit_type = buf[8];
  451.    png_read_oFFs(png_ptr, info, offset_x, offset_y, unit_type);
  452. }
  453. #endif
  454.  
  455. #if defined(PNG_READ_tIME_SUPPORTED)
  456. void
  457. png_handle_tIME(png_struct *png_ptr, png_info *info, png_uint_32 length)
  458. {
  459.    png_byte buf[7];
  460.    png_time mod_time;
  461.  
  462.    if (length != 7)
  463.    {
  464.       png_crc_skip(png_ptr, length);
  465.       return;
  466.    }
  467.  
  468.    png_crc_read(png_ptr, buf, 7);
  469.  
  470.    mod_time.second = buf[6];
  471.    mod_time.minute = buf[5];
  472.    mod_time.hour = buf[4];
  473.    mod_time.day = buf[3];
  474.    mod_time.month = buf[2];
  475.    mod_time.year = png_get_uint_16(buf);
  476.  
  477.    png_read_tIME(png_ptr, info, &mod_time);
  478. }
  479. #endif
  480.  
  481. #if defined(PNG_READ_tEXt_SUPPORTED)
  482. /* note: this does not correctly handle chunks that are > 64K */
  483. void
  484. png_handle_tEXt(png_struct *png_ptr, png_info *info, png_uint_32 length)
  485. {
  486.    char *key, *text;
  487.  
  488.    text = NULL;
  489.  
  490.    key = (char *)png_large_malloc(png_ptr, length + 1);
  491.    png_crc_read(png_ptr, (png_byte *)key, length);
  492.    key[(png_size_t)length] = '\0';
  493.  
  494.    for (text = key; *text; text++)
  495.       /* empty loop */ ;
  496.  
  497.    if (text != key + (png_size_t)length)
  498.       text++;
  499.  
  500.    png_read_tEXt(png_ptr, info, key, text, length - (text - key));
  501. }
  502. #endif
  503.  
  504. #if defined(PNG_READ_zTXt_SUPPORTED)
  505. /* note: this does not correctly handle chunks that are > 64K compressed */
  506. void
  507. png_handle_zTXt(png_struct *png_ptr, png_info *info, png_uint_32 length)
  508. {
  509.    char *key, *text;
  510.    int ret;
  511.    png_uint_32 text_size, key_size;
  512.  
  513.    text = NULL;
  514.  
  515.    key = png_large_malloc(png_ptr, length + 1);
  516.    png_crc_read(png_ptr, (png_byte *)key, length);
  517.    key[(png_size_t)length] = '\0';
  518.  
  519.    for (text = key; *text; text++)
  520.       /* empty loop */ ;
  521.  
  522.    /* zTXt can't have zero text */
  523.    if (text == key + (png_size_t)length)
  524.    {
  525.       png_large_free(png_ptr, key);
  526.       return;
  527.    }
  528.  
  529.    text++;
  530.  
  531.    if (*text) /* check compression byte */
  532.    {
  533.       png_large_free(png_ptr, key);
  534.       return;
  535.    }
  536.  
  537.    text++;
  538.  
  539.    png_ptr->zstream->next_in = (png_byte *)text;
  540.    png_ptr->zstream->avail_in = (uInt)(length - (text - key));
  541.    png_ptr->zstream->next_out = png_ptr->zbuf;
  542.    png_ptr->zstream->avail_out = (png_size_t)png_ptr->zbuf_size;
  543.  
  544.    key_size = text - key;
  545.    text_size = 0;
  546.    text = NULL;
  547.  
  548.    while (png_ptr->zstream->avail_in)
  549.    {
  550.       ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  551.       if (ret != Z_OK && ret != Z_STREAM_END)
  552.       {
  553.          inflateReset(png_ptr->zstream);
  554.          png_ptr->zstream->avail_in = 0;
  555.          png_large_free(png_ptr, key);
  556.          png_large_free(png_ptr, text);
  557.          return;
  558.       }
  559.       if (!png_ptr->zstream->avail_out || ret == Z_STREAM_END)
  560.       {
  561.          if (!text)
  562.          {
  563.             text = png_malloc(png_ptr,
  564.                png_ptr->zbuf_size - png_ptr->zstream->avail_out +
  565.                   key_size + 1);
  566.             memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
  567.                (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  568.             memcpy(text, key, (png_size_t)key_size);
  569.             text_size = key_size + (png_size_t)png_ptr->zbuf_size -
  570.                png_ptr->zstream->avail_out;
  571.             *(text + (png_size_t)text_size) = '\0';
  572.          }
  573.          else
  574.          {
  575.             char *tmp;
  576.  
  577.             tmp = text;
  578.             text = png_large_malloc(png_ptr, text_size +
  579.                png_ptr->zbuf_size - png_ptr->zstream->avail_out + 1);
  580.             memcpy(text, tmp, (png_size_t)text_size);
  581.             png_large_free(png_ptr, tmp);
  582.             memcpy(text + (png_size_t)text_size, png_ptr->zbuf,
  583.                (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  584.             text_size += png_ptr->zbuf_size - png_ptr->zstream->avail_out;
  585.             *(text + (png_size_t)text_size) = '\0';
  586.          }
  587.          if (ret != Z_STREAM_END)
  588.          {
  589.             png_ptr->zstream->next_out = png_ptr->zbuf;
  590.             png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  591.          }
  592.       }
  593.       else
  594.       {
  595.          break;
  596.       }
  597.  
  598.       if (ret == Z_STREAM_END)
  599.          break;
  600.    }
  601.  
  602.    inflateReset(png_ptr->zstream);
  603.    png_ptr->zstream->avail_in = 0;
  604.  
  605.    if (ret != Z_STREAM_END)
  606.    {
  607.       png_large_free(png_ptr, key);
  608.       png_large_free(png_ptr, text);
  609.       return;
  610.    }
  611.  
  612.    png_large_free(png_ptr, key);
  613.    key = text;
  614.    text += (png_size_t)key_size;
  615.    text_size -= key_size;
  616.  
  617.    png_read_zTXt(png_ptr, info, key, text, text_size, 0);
  618. }
  619. #endif
  620.  
  621. /* Combines the row recently read in with the previous row.
  622.    This routine takes care of alpha and transparency if requested.
  623.    This routine also handles the two methods of progressive display
  624.    of interlaced images, depending on the mask value.
  625.    The mask value describes which pixels are to be combined with
  626.    the row.  The pattern always repeats every 8 pixels, so just 8
  627.    bits are needed.  A one indicates the pixels is to be combined,
  628.    a zero indicates the pixel is to be skipped.  This is in addition
  629.    to any alpha or transparency value associated with the pixel.  If
  630.    you want all pixels to be combined, pass 0xff (255) in mask.
  631. */
  632. void
  633. png_combine_row(png_struct *png_ptr, png_byte *row,
  634.    int mask)
  635. {
  636.    if (mask == 0xff)
  637.    {
  638.       memcpy(row, png_ptr->row_buf + 1,
  639.          (png_size_t)((png_ptr->width *
  640.          png_ptr->row_info.pixel_depth + 7) >> 3));
  641.    }
  642.    else
  643.    {
  644.       switch (png_ptr->row_info.pixel_depth)
  645.       {
  646.          case 1:
  647.          {
  648.             png_byte *sp;
  649.             png_byte *dp;
  650.             int m;
  651.             int shift;
  652.             png_uint_32 i;
  653.             int value;
  654.  
  655.             sp = png_ptr->row_buf + 1;
  656.             dp = row;
  657.             shift = 7;
  658.             m = 0x80;
  659.             for (i = 0; i < png_ptr->width; i++)
  660.             {
  661.                if (m & mask)
  662.                {
  663.                   value = (*sp >> shift) & 0x1;
  664.                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  665.                   *dp |= (value << shift);
  666.                }
  667.  
  668.                if (shift == 0)
  669.                {
  670.                   shift = 7;
  671.                   sp++;
  672.                   dp++;
  673.                }
  674.                else
  675.                   shift--;
  676.  
  677.                if (m == 1)
  678.                   m = 0x80;
  679.                else
  680.                   m >>= 1;
  681.             }
  682.             break;
  683.          }
  684.          case 2:
  685.          {
  686.             png_byte *sp;
  687.             png_byte *dp;
  688.             int m;
  689.             int shift;
  690.             png_uint_32 i;
  691.             int value;
  692.  
  693.             sp = png_ptr->row_buf + 1;
  694.             dp = row;
  695.             shift = 6;
  696.             m = 0x80;
  697.             for (i = 0; i < png_ptr->width; i++)
  698.             {
  699.                if (m & mask)
  700.                {
  701.                   value = (*sp >> shift) & 0x3;
  702.                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  703.                   *dp |= (value << shift);
  704.                }
  705.  
  706.                if (shift == 0)
  707.                {
  708.                   shift = 6;
  709.                   sp++;
  710.                   dp++;
  711.                }
  712.                else
  713.                   shift -= 2;
  714.                if (m == 1)
  715.                   m = 0x80;
  716.                else
  717.                   m >>= 1;
  718.             }
  719.             break;
  720.          }
  721.          case 4:
  722.          {
  723.             png_byte *sp;
  724.             png_byte *dp;
  725.             int m;
  726.             int shift;
  727.             png_uint_32 i;
  728.             int value;
  729.  
  730.             sp = png_ptr->row_buf + 1;
  731.             dp = row;
  732.             shift = 4;
  733.             m = 0x80;
  734.             for (i = 0; i < png_ptr->width; i++)
  735.             {
  736.                if (m & mask)
  737.                {
  738.                   value = (*sp >> shift) & 0xf;
  739.                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  740.                   *dp |= (value << shift);
  741.                }
  742.  
  743.                if (shift == 0)
  744.                {
  745.                   shift = 4;
  746.                   sp++;
  747.                   dp++;
  748.                }
  749.                else
  750.                   shift -= 4;
  751.                if (m == 1)
  752.                   m = 0x80;
  753.                else
  754.                   m >>= 1;
  755.             }
  756.             break;
  757.          }
  758.          default:
  759.          {
  760.             png_byte *sp;
  761.             png_byte *dp;
  762.             png_uint_32 i;
  763.             int pixel_bytes, m;
  764.  
  765.             pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  766.  
  767.             sp = png_ptr->row_buf + 1;
  768.             dp = row;
  769.             m = 0x80;
  770.             for (i = 0; i < png_ptr->width; i++)
  771.             {
  772.                if (m & mask)
  773.                {
  774.                   memcpy(dp, sp, pixel_bytes);
  775.                }
  776.  
  777.                sp += pixel_bytes;
  778.                dp += pixel_bytes;
  779.  
  780.                if (m == 1)
  781.                   m = 0x80;
  782.                else
  783.                   m >>= 1;
  784.             }
  785.             break;
  786.          }
  787.       }
  788.    }
  789. }
  790.  
  791. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  792. void
  793. png_do_read_interlace(png_row_info *row_info, png_byte *row, int pass)
  794. {
  795.    if (row && row_info)
  796.    {
  797.       png_uint_32 final_width;
  798.  
  799.       final_width = row_info->width * png_pass_inc[pass];
  800.  
  801.       switch (row_info->pixel_depth)
  802.       {
  803.          case 1:
  804.          {
  805.             png_byte *sp, *dp;
  806.             int sshift, dshift;
  807.             png_byte v;
  808.             png_uint_32 i;
  809.             int j;
  810.  
  811.             sp = row + (png_size_t)((row_info->width - 1) >> 3);
  812.             sshift = 7 - (int)((row_info->width + 7) & 7);
  813.             dp = row + (png_size_t)((final_width - 1) >> 3);
  814.             dshift = 7 - (int)((final_width + 7) & 7);
  815.             for (i = row_info->width; i; i--)
  816.             {
  817.                v = (*sp >> sshift) & 0x1;
  818.                for (j = 0; j < png_pass_inc[pass]; j++)
  819.                {
  820.                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  821.                   *dp |= (png_byte)(v << dshift);
  822.                   if (dshift == 7)
  823.                   {
  824.                      dshift = 0;
  825.                      dp--;
  826.                   }
  827.                   else
  828.                      dshift++;
  829.                }
  830.                if (sshift == 7)
  831.                {
  832.                   sshift = 0;
  833.                   sp--;
  834.                }
  835.                else
  836.                   sshift++;
  837.             }
  838.             break;
  839.          }
  840.          case 2:
  841.          {
  842.             png_byte *sp, *dp;
  843.             int sshift, dshift;
  844.             png_byte v;
  845.             png_uint_32 i, j;
  846.  
  847.             sp = row + (png_size_t)((row_info->width - 1) >> 2);
  848.             sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1);
  849.             dp = row + (png_size_t)((final_width - 1) >> 2);
  850.             dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
  851.             for (i = row_info->width; i; i--)
  852.             {
  853.                v = (*sp >> sshift) & 0x3;
  854.                for (j = 0; j < png_pass_inc[pass]; j++)
  855.                {
  856.                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  857.                   *dp |= (v << dshift);
  858.                   if (dshift == 6)
  859.                   {
  860.                      dshift = 0;
  861.                      dp--;
  862.                   }
  863.                   else
  864.                      dshift += 2;
  865.                }
  866.                if (sshift == 6)
  867.                {
  868.                   sshift = 0;
  869.                   sp--;
  870.                }
  871.                else
  872.                   sshift += 2;
  873.             }
  874.             break;
  875.          }
  876.          case 4:
  877.          {
  878.             png_byte *sp, *dp;
  879.             int sshift, dshift;
  880.             png_byte v;
  881.             png_uint_32 i;
  882.             int j;
  883.  
  884.             sp = row + (png_size_t)((row_info->width - 1) >> 1);
  885.             sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2);
  886.             dp = row + (png_size_t)((final_width - 1) >> 1);
  887.             dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
  888.             for (i = row_info->width; i; i--)
  889.             {
  890.                v = (*sp >> sshift) & 0xf;
  891.                for (j = 0; j < png_pass_inc[pass]; j++)
  892.                {
  893.                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  894.                   *dp |= (v << dshift);
  895.                   if (dshift == 4)
  896.                   {
  897.                      dshift = 0;
  898.                      dp--;
  899.                   }
  900.                   else
  901.                      dshift = 4;
  902.                }
  903.                if (sshift == 4)
  904.                {
  905.                   sshift = 0;
  906.                   sp--;
  907.                }
  908.                else
  909.                   sshift = 4;
  910.             }
  911.             break;
  912.          }
  913.          default:
  914.          {
  915.             png_byte *sp, *dp;
  916.             png_byte v[8];
  917.             png_uint_32 i;
  918.             int j;
  919.             int pixel_bytes;
  920.  
  921.             pixel_bytes = (row_info->pixel_depth >> 3);
  922.  
  923.             sp = row + (png_size_t)((row_info->width - 1) * pixel_bytes);
  924.             dp = row + (png_size_t)((final_width - 1) * pixel_bytes);
  925.             for (i = row_info->width; i; i--)
  926.             {
  927.                memcpy(v, sp, pixel_bytes);
  928.                for (j = 0; j < png_pass_inc[pass]; j++)
  929.                {
  930.                   memcpy(dp, v, pixel_bytes);
  931.                   dp -= pixel_bytes;
  932.                }
  933.                sp -= pixel_bytes;
  934.             }
  935.             break;
  936.          }
  937.       }
  938.       row_info->width = final_width;
  939.       row_info->rowbytes = ((final_width *
  940.          (png_uint_32)row_info->pixel_depth + 7) >> 3);
  941.    }
  942. }
  943. #endif
  944.  
  945. void
  946. png_read_filter_row(png_row_info *row_info, png_byte *row,
  947.    png_byte *prev_row, int filter)
  948. {
  949.    switch (filter)
  950.    {
  951.       case 0:
  952.          break;
  953.       case 1:
  954.       {
  955.          png_uint_32 i;
  956.          int bpp;
  957.          png_byte *rp;
  958.          png_byte *lp;
  959.  
  960.          bpp = (row_info->pixel_depth + 7) / 8;
  961.          for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
  962.             i < row_info->rowbytes; i++, rp++, lp++)
  963.          {
  964.             *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
  965.          }
  966.          break;
  967.       }
  968.       case 2:
  969.       {
  970.          png_uint_32 i;
  971.          png_byte *rp;
  972.          png_byte *pp;
  973.  
  974.          for (i = 0, rp = row, pp = prev_row;
  975.             i < row_info->rowbytes; i++, rp++, pp++)
  976.          {
  977.             *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
  978.          }
  979.          break;
  980.       }
  981.       case 3:
  982.       {
  983.          png_uint_32 i;
  984.          int bpp;
  985.          png_byte *rp;
  986.          png_byte *pp;
  987.          png_byte *lp;
  988.  
  989.          bpp = (row_info->pixel_depth + 7) / 8;
  990.          for (i = 0, rp = row, pp = prev_row;
  991.             i < (png_uint_32)bpp; i++, rp++, pp++)
  992.          {
  993.             *rp = (png_byte)(((int)(*rp) +
  994.                ((int)(*pp) / 2)) & 0xff);
  995.          }
  996.          for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++)
  997.          {
  998.             *rp = (png_byte)(((int)(*rp) +
  999.                (int)(*pp + *lp) / 2) & 0xff);
  1000.          }
  1001.          break;
  1002.       }
  1003.       case 4:
  1004.       {
  1005.          int bpp;
  1006.          png_uint_32 i;
  1007.          png_byte *rp;
  1008.          png_byte *pp;
  1009.          png_byte *lp;
  1010.          png_byte *cp;
  1011.  
  1012.          bpp = (row_info->pixel_depth + 7) / 8;
  1013.          for (i = 0, rp = row, pp = prev_row,
  1014.             lp = row - bpp, cp = prev_row - bpp;
  1015.             i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  1016.          {
  1017.             int a, b, c, pa, pb, pc, p;
  1018.  
  1019.             b = *pp;
  1020.             if (i >= (png_uint_32)bpp)
  1021.             {
  1022.                c = *cp;
  1023.                a = *lp;
  1024.             }
  1025.             else
  1026.             {
  1027.                a = c = 0;
  1028.             }
  1029.             p = a + b - c;
  1030.             pa = abs(p - a);
  1031.             pb = abs(p - b);
  1032.             pc = abs(p - c);
  1033.  
  1034.             if (pa <= pb && pa <= pc)
  1035.                p = a;
  1036.             else if (pb <= pc)
  1037.                p = b;
  1038.             else
  1039.                p = c;
  1040.  
  1041.             *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  1042.          }
  1043.          break;
  1044.       }
  1045.       default:
  1046.          break;
  1047.    }
  1048. }
  1049.  
  1050. void
  1051. png_read_finish_row(png_struct *png_ptr)
  1052. {
  1053.    png_ptr->row_number++;
  1054.    if (png_ptr->row_number < png_ptr->num_rows)
  1055.       return;
  1056.  
  1057.    if (png_ptr->interlaced)
  1058.    {
  1059.       png_ptr->row_number = 0;
  1060.       memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1061.       do
  1062.       {
  1063.          png_ptr->pass++;
  1064.          if (png_ptr->pass >= 7)
  1065.             break;
  1066.          png_ptr->iwidth = (png_ptr->width +
  1067.             png_pass_inc[png_ptr->pass] - 1 -
  1068.             png_pass_start[png_ptr->pass]) /
  1069.             png_pass_inc[png_ptr->pass];
  1070.          png_ptr->irowbytes = ((png_ptr->iwidth *
  1071.             png_ptr->pixel_depth + 7) >> 3) + 1;
  1072.          if (!(png_ptr->transformations & PNG_INTERLACE))
  1073.          {
  1074.             png_ptr->num_rows = (png_ptr->height +
  1075.                png_pass_yinc[png_ptr->pass] - 1 -
  1076.                png_pass_ystart[png_ptr->pass]) /
  1077.                png_pass_yinc[png_ptr->pass];
  1078.             if (!(png_ptr->num_rows))
  1079.                continue;
  1080.          }
  1081.          if (png_ptr->transformations & PNG_INTERLACE)
  1082.             break;
  1083.       } while (png_ptr->iwidth == 0);
  1084.  
  1085.       if (png_ptr->pass < 7)
  1086.          return;
  1087.    }
  1088.  
  1089.    if (!png_ptr->zlib_finished)
  1090.    {
  1091.       char extra;
  1092.       int ret;
  1093.  
  1094.       png_ptr->zstream->next_out = (Byte *)&extra;
  1095.       png_ptr->zstream->avail_out = (uInt)1;
  1096.       do
  1097.       {
  1098.          if (!(png_ptr->zstream->avail_in))
  1099.          {
  1100.             while (!png_ptr->idat_size)
  1101.             {
  1102.                png_byte buf[4];
  1103.                png_uint_32 crc;
  1104.  
  1105.                png_read_data(png_ptr, buf, 4);
  1106.                crc = png_get_uint_32(buf);
  1107.                if (((crc ^ 0xffffffffL) & 0xffffffffL) !=
  1108.                   (png_ptr->crc & 0xffffffffL))
  1109.                   png_error(png_ptr, "Bad CRC value");
  1110.  
  1111.                png_read_data(png_ptr, buf, 4);
  1112.                png_ptr->idat_size = png_get_uint_32(buf);
  1113.                png_reset_crc(png_ptr);
  1114.  
  1115.                png_crc_read(png_ptr, buf, 4);
  1116.                if (memcmp(buf, png_IDAT, 4))
  1117.                   png_error(png_ptr, "Not enough image data");
  1118.  
  1119.             }
  1120.             png_ptr->zstream->avail_in = (uInt)png_ptr->zbuf_size;
  1121.             png_ptr->zstream->next_in = png_ptr->zbuf;
  1122.             if (png_ptr->zbuf_size > png_ptr->idat_size)
  1123.                png_ptr->zstream->avail_in = (uInt)png_ptr->idat_size;
  1124.             png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in);
  1125.             png_ptr->idat_size -= png_ptr->zstream->avail_in;
  1126.          }
  1127.          ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  1128.          if (ret == Z_STREAM_END)
  1129.          {
  1130.             if (!(png_ptr->zstream->avail_out) || png_ptr->zstream->avail_in ||
  1131.                png_ptr->idat_size)
  1132.                png_error(png_ptr, "Extra compressed data");
  1133.             png_ptr->mode = PNG_AT_LAST_IDAT;
  1134.             break;
  1135.          }
  1136.          if (ret != Z_OK)
  1137.             png_error(png_ptr, "Compression Error");
  1138.  
  1139.          if (!(png_ptr->zstream->avail_out))
  1140.             png_error(png_ptr, "Extra compressed data");
  1141.  
  1142.       } while (1);
  1143.       png_ptr->zstream->avail_out = 0;
  1144.    }
  1145.  
  1146.    if (png_ptr->idat_size || png_ptr->zstream->avail_in)
  1147.       png_error(png_ptr, "Extra compression data");
  1148.  
  1149.    inflateReset(png_ptr->zstream);
  1150.  
  1151.    png_ptr->mode = PNG_AT_LAST_IDAT;
  1152. }
  1153.  
  1154. void
  1155. png_read_start_row(png_struct *png_ptr)
  1156. {
  1157.    int max_pixel_depth;
  1158.    png_uint_32 rowbytes;
  1159.  
  1160.    png_ptr->zstream->avail_in = 0;
  1161.    png_init_read_transformations(png_ptr);
  1162.    if (png_ptr->interlaced)
  1163.    {
  1164.       if (!(png_ptr->transformations & PNG_INTERLACE))
  1165.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1166.             png_pass_ystart[0]) / png_pass_yinc[0];
  1167.       else
  1168.          png_ptr->num_rows = png_ptr->height;
  1169.  
  1170.       png_ptr->iwidth = (png_ptr->width +
  1171.          png_pass_inc[png_ptr->pass] - 1 -
  1172.          png_pass_start[png_ptr->pass]) /
  1173.          png_pass_inc[png_ptr->pass];
  1174.       png_ptr->irowbytes = ((png_ptr->iwidth *
  1175.          png_ptr->pixel_depth + 7) >> 3) + 1;
  1176.    }
  1177.    else
  1178.    {
  1179.       png_ptr->num_rows = png_ptr->height;
  1180.       png_ptr->iwidth = png_ptr->width;
  1181.       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  1182.    }
  1183.  
  1184.    max_pixel_depth = png_ptr->pixel_depth;
  1185.  
  1186. #if defined(PNG_READ_PACK_SUPPORTED)
  1187.    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  1188.    {
  1189.       max_pixel_depth = 8;
  1190.    }
  1191. #endif
  1192.  
  1193. #if defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_PACK_SUPPORTED)
  1194.    if (png_ptr->transformations & (PNG_EXPAND | PNG_PACK))
  1195.    {
  1196.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1197.       {
  1198.          if (png_ptr->num_trans)
  1199.             max_pixel_depth = 32;
  1200.          else
  1201.             max_pixel_depth = 24;
  1202.       }
  1203.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1204.       {
  1205.          if (max_pixel_depth < 8)
  1206.             max_pixel_depth = 8;
  1207.          if (png_ptr->num_trans)
  1208.             max_pixel_depth *= 2;
  1209.       }
  1210.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1211.       {
  1212.          if (png_ptr->num_trans)
  1213.          {
  1214.             max_pixel_depth *= 4;
  1215.             max_pixel_depth /= 3;
  1216.          }
  1217.       }
  1218.    }
  1219. #endif
  1220.  
  1221. #if defined(PNG_READ_FILLER_SUPPORTED)
  1222.    if (png_ptr->transformations & (PNG_FILLER))
  1223.    {
  1224.       if (max_pixel_depth < 32)
  1225.          max_pixel_depth = 32;
  1226.    }
  1227. #endif
  1228.  
  1229. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1230.    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1231.    {
  1232.       if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  1233.          png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1234.       {
  1235.          if (max_pixel_depth <= 16)
  1236.             max_pixel_depth = 32;
  1237.          else if (max_pixel_depth <= 32)
  1238.             max_pixel_depth = 64;
  1239.       }
  1240.       else
  1241.       {
  1242.          if (max_pixel_depth <= 8)
  1243.             max_pixel_depth = 24;
  1244.          else if (max_pixel_depth <= 16)
  1245.             max_pixel_depth = 48;
  1246.       }
  1247.    }
  1248. #endif
  1249.  
  1250.    /* align the width on the next larger 8 pixels.  Mainly used
  1251.       for interlacing */
  1252.    rowbytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  1253.    /* calculate the maximum bytes needed, adding a byte and a pixel
  1254.       for safety sake */
  1255.    rowbytes = ((rowbytes * (png_uint_32)max_pixel_depth + 7) >> 3) +
  1256.       1 + ((max_pixel_depth + 7) >> 3);
  1257. #ifdef PNG_MAX_MALLOC_64K
  1258.    if (rowbytes > 65536L)
  1259.       png_error(png_ptr, "This image requires a row greater then 64KB");
  1260. #endif
  1261.    png_ptr->row_buf = (png_byte *)png_large_malloc(png_ptr, rowbytes);
  1262.  
  1263. #ifdef PNG_MAX_MALLOC_64K
  1264.    if (png_ptr->rowbytes + 1 > 65536L)
  1265.       png_error(png_ptr, "This image requires a row greater then 64KB");
  1266. #endif
  1267.    png_ptr->prev_row = png_large_malloc(png_ptr,
  1268.       png_ptr->rowbytes + 1);
  1269.  
  1270.    memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1271.  
  1272.    png_ptr->row_init = 1;
  1273. }
  1274.  
  1275.